<?php
declare (strict_types = 1);

namespace app\common\service;

use app\admin\service\BaseService;
use app\admin\service\ConfigService;
use app\common\model\EmailSendLogModel;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

/**
 * 邮件服务
 *
 * @Author tanruizheng
 * @DateTime 2021-07-19
 */
class EmailService extends BaseService
{

    /**
     * 发送邮件
     *
     * @Author tanruizheng
     * @DateTime 2021-07-22
     * @return void
     */
    public static function sendEmail($toEmail,$subject,$content){
        
        $mail = new PHPMailer(true);
        $config = ConfigService::getConfig()['data'];
        $data = [
            'to_email'      => $toEmail,
            'from_email'    => $config['mail_user'],
            'subject'       => $subject,
            'content'       => $content,
            'status'        => 1,
            'send_time'     => time()
        ];
        $emailSendLogModel = new EmailSendLogModel();
        try {
           
            //Server settings                   //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host       = $config['mail_server'];                 //Set the SMTP server to send through
            $mail->SMTPAuth   = true;              //Enable SMTP authentication
            // $mail->Username   = '1556050788@qq.com';                   //SMTP username
            // $mail->Password   = 'qxkhlrckvgbmifci';               //SMTP password
             // $mail->Username   = '670600971@qq.com';                   //SMTP username
            // $mail->Password   = 'iennznncvkkabcgh';               //SMTP password
            $mail->Username   = $config['mail_user'];                   //SMTP username
            $mail->Password   = $config['mail_password'];               //SMTP password
            $mail->SMTPSecure = 'ssl';            //Enable implicit TLS encryption
            $mail->Port       = $config['mail_port'];                   //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
            $mail->CharSet = 'UTF-8';
            //Recipients
            $mail->setFrom($config['mail_user'],$config['mail_fname']);
            $mail->addAddress($toEmail);     //Add a recipient
        
            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $content;
        
            $res = $mail->send();
            if($res){
                $emailSendLogModel->create($data);
                return self::createSuccess('发送成功');
            }else{
                $data['status'] = 0;
                $data['error_msg'] = '发送失败';
                $emailSendLogModel->create($data);
                return self::createSuccess('发送失败');
            }
        } catch (Exception $e) {
            $data['status'] = 0;
            $data['error_msg'] = $mail->ErrorInfo;
            $emailSendLogModel->create($data);
            return self::createFail($mail->ErrorInfo);
        }
    }

    /**
     * 获取发送邮件日志
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $where
     * @return void
     */
    public static function getSendLogList($where = [], $page = 1, $limit = 15){
        $list = EmailSendLogModel::where($where)->page($page,$limit)->select()->toArray();
        foreach ($list as &$item) {
            $item['send_time'] = $item['send_time'] ? date('Y-m-d H:i:s',$item['send_time']) : '';
        }
        $count = EmailSendLogModel::where($where)->count();
        return self::createList($list,$count);
    }
}
